package com.us.filter;

import java.lang.reflect.Method;

import org.nutz.mvc.ActionContext;
import org.nutz.mvc.ActionFilter;
import org.nutz.mvc.View;
import org.nutz.mvc.view.ServerRedirectView;

import com.us.core.UserAction;
import com.us.model.AuthModel;

public class FrontActionFilter implements ActionFilter {

    View logon = new ServerRedirectView("/user/logon");

    @Override
    public View match(ActionContext ac) {
        Method invok = ac.getMethod();
        Class<?> actionCls = invok.getDeclaringClass();
        try {
            if (actionCls.newInstance() instanceof AuthModel) {
                Object user = ac.getRequest().getSession().getAttribute(UserAction.USER_SESSION_KEY);
                if (user == null) {
                    return logon;
//                    At at = invok.getAnnotation(At.class);
//                    ac.getResponse().sendRedirect("/~/index.do");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
